package com.centit.framework.ip.agentofd.service.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Map;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.centit.framework.jdbc.dao.DatabaseOptUtils;
import com.centit.support.database.utils.PageDesc;
 
import com.centit.framework.jdbc.service.BaseEntityManagerImpl;




import com.centit.framework.ip.agentofd.po.OldNewfileinfo;
import com.centit.framework.ip.agentofd.dao.OldNewfileinfoDao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.centit.framework.ip.agentofd.service.OldNewfileinfoManager;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
/**
 * OldNewfileinfo  Service.
 * create by scaffold 2018-07-30 
 * @author codefan@sina.com
 * 新旧文件转换映射信息表新旧文件转换映射信息表   
*/
@Service
public class OldNewfileinfoManagerImpl 
		extends BaseEntityManagerImpl<OldNewfileinfo,java.lang.String,OldNewfileinfoDao>
	implements OldNewfileinfoManager{

	public static final Log log = LogFactory.getLog(OldNewfileinfoManager.class);

	
	private OldNewfileinfoDao oldNewfileinfoDao ;
	
	@Resource(name = "oldNewfileinfoDao")
    @NotNull
	public void setOldNewfileinfoDao(OldNewfileinfoDao baseDao)
	{
		this.oldNewfileinfoDao = baseDao;
		setBaseDao(this.oldNewfileinfoDao);
	}
	
/*
 	@PostConstruct
    public void init() {
        
    }
 	
 */
	@Override
    @Transactional(propagation=Propagation.REQUIRED) 
	public JSONArray listOldNewfileinfosAsJson(
            String[] fields,
            Map<String, Object> filterMap, PageDesc pageDesc){
			
		  return null;
	}

	@Override
	public JSONObject loadsfwAll() {
         String sql="select data_value,to_char(numc) from  f_datadictionary " +
				 " left join (select itemstate,count(1) numc from T_TODO_ITEM " +
				 " group by itemstate) on itemstate=data_code " +
				 " where catalog_code='ITEMSTATE' and data_code in('sw','fw')";
		Object[] params=null;
         String[] fieldnames=new String[]{"name","value"};
		JSONArray jsonArray= DatabaseOptUtils.listObjectsBySqlAsJson(baseDao,sql,fieldnames,params);
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("data",jsonArray);
		//jsonArray一般不会为空的
		if(jsonArray!=null&&jsonArray.size()>0){
			JSONObject jsonObjectdata=null;
			jsonObjectdata=jsonArray.getJSONObject(0);
			jsonObject.put("fw",jsonObjectdata.get("value")==null?"0":jsonObjectdata.get("value"));
			jsonObjectdata=jsonArray.getJSONObject(1);
			jsonObject.put("sw",jsonObjectdata.get("value")==null?"0":jsonObjectdata.get("value"));
			int all=Integer.parseInt((String)jsonObject.get("sw"))+Integer.parseInt((String)jsonObject.get("fw"));
			jsonObject.put("all",String.valueOf(all));
			double  fenzi=0;
			double fenmu=1;
			fenzi=Integer.parseInt((String)jsonObject.get("sw"));
			fenmu=Integer.parseInt((String)jsonObject.get("all"));
			if(fenmu>0){

			}else{
				fenmu=1;
			}
			BigDecimal bg = new BigDecimal((fenzi/fenmu)*100).setScale(2, RoundingMode.HALF_UP);
			jsonObject.put("swb",bg.doubleValue()+"%");
			fenzi=Integer.parseInt((String)jsonObject.get("fw"));
			bg = new BigDecimal((fenzi/fenmu)*100).setScale(2, RoundingMode.HALF_UP);
			jsonObject.put("fwb",bg.doubleValue()+"%");
		}else {
			jsonObject.put("sw",0);
			jsonObject.put("swb",0);
			jsonObject.put("fw",0);
			jsonObject.put("fwb",0);
			jsonObject.put("all",0);
		}
		return jsonObject;
	}
	@Override
	public JSONObject sfwCurrDay() {
		String sql="select data_value,to_char(numc) from  f_datadictionary " +
				" left join (select itemstate,count(1) numc from T_TODO_ITEM where to_char(createtime,'yyyy-MM-dd')=to_char(sysdate,'yyyy-MM-dd')" +
				" group by itemstate) on itemstate=data_code " +
				" where catalog_code='ITEMSTATE' and data_code in('sw','fw')";
		Object[] params=null;
		String[] fieldnames=new String[]{"name","value"};
		JSONArray jsonArray= DatabaseOptUtils.listObjectsBySqlAsJson(baseDao,sql,fieldnames,params);
		JSONObject jsonObject=new JSONObject();
		jsonObject.put("data",jsonArray);
		//jsonArray一般不会为空的
		if(jsonArray!=null&&jsonArray.size()>0){
			JSONObject jsonObjectdata=null;
			jsonObjectdata=jsonArray.getJSONObject(0);
			jsonObject.put("fw",jsonObjectdata.get("value")==null?"0":jsonObjectdata.get("value"));
			jsonObjectdata=jsonArray.getJSONObject(1);
			jsonObject.put("sw",jsonObjectdata.get("value")==null?"0":jsonObjectdata.get("value"));
			int all=Integer.parseInt((String)jsonObject.get("sw"))+Integer.parseInt((String)jsonObject.get("fw"));
			jsonObject.put("all",String.valueOf(all));
			double  fenzi=0;
			double fenmu=1;
			fenzi=Integer.parseInt((String)jsonObject.get("sw"));
			fenmu=Integer.parseInt((String)jsonObject.get("all"));
			if(fenmu>0){

			}else{
				fenmu=1;
			}
			BigDecimal bg = new BigDecimal((fenzi/fenmu)*100).setScale(2, RoundingMode.HALF_UP);
			jsonObject.put("swb",bg.doubleValue()+"%");
			fenzi=Integer.parseInt((String)jsonObject.get("fw"));
			bg = new BigDecimal((fenzi/fenmu)*100).setScale(2, RoundingMode.HALF_UP);
			jsonObject.put("fwb",bg.doubleValue()+"%");
		}else {
			jsonObject.put("sw",0);
			jsonObject.put("swb",0);
			jsonObject.put("fw",0);
			jsonObject.put("fwb",0);
			jsonObject.put("all",0);
		}
		return jsonObject;
	}

}

